package com.redisson.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by cheng on 2019/9/2.
 */
public class BaseTest {
    @Autowired
    private DynamicToSpring dynamicToSpring;
    private static final String CHANGE_COMMAND = "$$";
    private static final String END = "##";
    private static final String CLEAR = "--";
    private static final String ALL = "**";
    private Object nowTest;
    private BufferedReader reader;


    public void test() throws InvocationTargetException, IllegalAccessException, IOException {
        nowTest = this;
        clearFile();
        newReader();
        System.out.println("启动完成请输入要测试的方法名称：");
        String methodName = null;
        while((methodName = readLine()) != null) {
            try {
                if(methodName.isEmpty()){//空行忽略
                    continue;
                }
                if (methodName.equals(END)) {
                    break;
                }
                if (methodName.equals(CHANGE_COMMAND)) {
                    change();
                    continue;
                }
                //清理文件
                if(CLEAR.equals(methodName)) {
                    clear();
                    continue;
                }

                if(ALL.equals(methodName)){
                    testAll();
                    continue;
                }

                Method method = getCallMethod(methodName.trim());
                if(method == null){
                    System.out.println(System.lineSeparator()+System.lineSeparator()+System.lineSeparator()+"不存在的方法："+methodName);
                    continue;
                }
                System.out.println(System.lineSeparator()+System.lineSeparator()+System.lineSeparator()+"开始调用方法："+methodName);
                method.invoke(nowTest, null);
            }catch (Throwable t){
                t.printStackTrace();
            }
        }
    }

    public String readLine(){
        try{
            while (true) {
                String methodName = reader.readLine();
                if (StringUtils.isEmpty(methodName) || methodName.equals(System.lineSeparator())) {
                    Thread.sleep(100);
                    continue;
                }
                methodName = methodName.trim();
                System.out.println(methodName);
                if (methodName.equals(END)) {
                    reader.close();
                    return methodName;
                }
                return methodName;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "##";
    }
    private Method getCallMethod(String methodName){
        Method[] methods = nowTest.getClass().getMethods();
        for(Method method : methods){
            if(method.getName().equals(methodName)){
                return method;
            }
        }
        return null;
    }

    public static void  main(String[] args){
        System.out.println(new BaseTest().getInputFile());
    }
    protected String getInputFile(){
        getHostName();
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        return desktopPath + File.separator + "input.log";
    }

    public static String getHostName() {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            throw new IllegalStateException(e);
        }
        String ip=addr.getHostAddress().toString(); //获取本机ip
        String hostName=addr.getHostName().toString(); //获取本机计算机名称
        System.out.println("本机IP："+ip+"\n本机名称:"+hostName);
        return hostName;
    }

    public void clearFile(){

        File file =new File(getInputFile());
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void change() {
        System.out.println("请输入要切换的测试类型");
        String className = readLine();
        Class clazz = getClass(className);
        if(className != null){
            nowTest = dynamicToSpring.createBean(clazz);
        }else{
            System.out.println("切换失败");
        }

    }

    private Class getClass(String className){
        try {
            Class clazz = Class.forName(className);
            System.out.println("切换成功"+className);
            return clazz;
        } catch (Throwable e) {
            //e.printStackTrace();
            className = nowTest.getClass().getPackage().getName() + "." + className;
            System.out.println("类型不存在,尝试从当期目录找"+className);
            try {
                Class clazz =  Class.forName(className);
                System.out.println("切换成功"+className);
                return clazz;
            } catch (ClassNotFoundException e1) {
                System.out.println(className + "类型不存在");
                //e1.printStackTrace();
            }
        }
        return null;
    }

    private void clear() throws IOException {
        reader.close();
        clearFile();
        newReader();

    }

    private void newReader() throws IOException {
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(getInputFile())));
    }

    private void testAll(){
        Method[] methods = nowTest.getClass().getMethods();
        for(Method method : methods){
            if(method.getParameterCount() > 0){
                continue;
            }
            if(!method.getDeclaringClass().equals(nowTest.getClass())){
                continue;
            }
            if(method.getName().equals("test")){
                continue;
            }
            try {
                System.out.println(System.lineSeparator()+System.lineSeparator()+System.lineSeparator()+"开始调用方法："+ method.getName());
                method.invoke(nowTest,null);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

}
